package com.qf.xiaomi.web.base;

import com.qf.xiaomi.utils.StringConstans;
import com.qf.xiaomi.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * Created by 田贵宾 on 2022/8/21 16:50
 *
 * @author 田贵宾
 * @version 1.0
 * //TODO java练习阶段
 * @since 1.0
 */

public class AdminBaseServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String flagName = req.getParameter("flag");
        Class<? extends AdminBaseServlet> aClass = this.getClass();
        try {
            Method method = aClass.getMethod(flagName,HttpServletRequest.class,HttpServletResponse.class);
            String result = (String) method.invoke(this, req, resp);
           if(result!=null){
               String url = result.split(":")[1];
               if (result.startsWith(StringConstans.FORWARD)){
                   req.getRequestDispatcher(url).forward(req,resp);
               }else if(result.startsWith(StringConstans.REDIRECT)){
                   resp.sendRedirect(url);
               }
           }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
